﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Video;
using IAlarm;
using Player;
using IVideo;

namespace ForestFirePreventionFramework
{
    public partial class FormFramework : Form
    {
        Dictionary<string, List<string>> devices;
        List<VideoBase> videos = new List<VideoBase>();
        UserVideo video;

        Dictionary<VideoBase, GroupInfo> alarmsGroup = new Dictionary<VideoBase, GroupInfo>();
        Dictionary<VideoBase, List<Bitmap>> alarmsInfo = new Dictionary<VideoBase, List<Bitmap>>();
        Dictionary<VideoBase, int> alarmsIndex = new Dictionary<VideoBase, int>();
        public FormFramework()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //this.devices = VideoBase.GetAreaDeviceList();
            this.devices = PlayerBase.GetAreaDeviceList();

            foreach (KeyValuePair<string, List<string>> area in devices)
            {
                TreeNode TNArea = new TreeNode(area.Key);
                foreach (string device in area.Value)
                {
                    TreeNode TNDevice = new TreeNode(device);
                    TNArea.Nodes.Add(TNDevice);
                }
                tVAreaDevices.Nodes.Add(TNArea);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            foreach (VideoBase video in this.videos)
            {
                if (video.IsOpened)
                    video.CloseVideo();
                if (video.IsLogined)
                    video.LogoutEncoder();
            }

            base.OnClosing(e);
        }

        private void tVAreaDevices_DoubleClick(object sender, EventArgs e)
        {

            TreeNode TN = tVAreaDevices.SelectedNode;
            if (TN == null) return;

            if (TN.Level == 1)
            {
                if (TN.Tag == null)
                {//新建
                    video = new UserVideo();
                    video.Initialize(TN.Parent.Text, TN.Text);
                    video.Alarm += new EventHandler<AlarmEventArgs>(video_AlarmCallback);
                    video.ReceiveHorizon += new EventHandler<IVideo.HorizonEventArgs>(video_HorizonCallback);
                    video.ReceiveVertical += new EventHandler<IVideo.VerticalEventArgs>(video_ReceiveVertical);

                    this.alarmsGroup.Add(video, null);
                    this.alarmsInfo.Add(video, new List<Bitmap>());
                    this.alarmsIndex.Add(video, 0);

                    if (video.LoginEncoder())//登陆
                    {
                        if (video.OpenVideo())//开启
                        {
                            int x = videos.Count % 2;
                            int y = videos.Count / 2;
                            video.SetBounds(x * 352, y * 288, 352, 288);
                            this.tPRealVideo.Controls.Add(video);

                            UCShowDecode control = new UCShowDecode();
                            control.SetBounds(x * 352, y * 288, 352, 288);
                            this.tPVideoDecode.Controls.Add(control);

                            this.video.DecodeShow = control;

                            TN.Tag = video;
                            videos.Add(video);
                        }
                        else
                        {
                            MessageBox.Show("打开视频失败");
                        }
                    }
                    else
                        MessageBox.Show("登陆编码器失败");
                }
                else
                {//开关
                    video = TN.Tag as UserVideo;

                    if (video.IsOpened)
                    {
                        video.CloseVideo();    //关

                        video.Invalidate();     //刷新
                    }
                    else
                        video.OpenVideo();     //开
                }
            }
            else if (TN.Level == 2)
            {
                if (TN.Tag == null || TN.Parent.Tag == null) return;
                GroupInfo alarmGroup = TN.Tag as GroupInfo;
                VideoBase video = TN.Parent.Tag as VideoBase;

                if (alarmGroup == null || video == null) return;

                this.alarmsGroup[video] = alarmGroup;
            }
        }

        Font font = new Font("宋体", 20f);
        void video_AlarmCallback(object sender, AlarmEventArgs e)
        {
           

        }

        delegate void AddAlarmCallback(TreeNode tnVideo, TreeNode tnAlarm);
        void AddAlarmCall(TreeNode tnVideo, TreeNode tnAlarm)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new AddAlarmCallback(this.AddAlarmCall), new object[] { tnVideo, tnAlarm });
            }
            else
            {
                tnVideo.Nodes.Add(tnAlarm);
            }
        }

        private void ucShowDecode1_Paint(object sender, PaintEventArgs e)
        {
            int index = 0;
            foreach (VideoBase video in this.alarmsInfo.Keys)
            {
                if (this.alarmsInfo[video].Count == 0) continue;

                int x = index % 2;
                int y = index / 2;
                e.Graphics.DrawImage(this.alarmsInfo[video][this.alarmsIndex[video]], new Rectangle(x * 352, y * 288, 352, 288));
                index++;
                this.alarmsIndex[video]++;
                this.alarmsIndex[video] %= this.alarmsInfo[video].Count;
            }
            //try
            //{
            //    int index = 0;
            //    foreach (var item in this.alarmsInfo.Keys)
            //    {
            //        if (this.alarmsInfo[item] == null) continue;
            //        IEnumerator<AlarmInfo> alarms = this.alarmsInfo[item];
            //        Bitmap image = null;
            //        if (alarms.MoveNext())
            //        {
            //            image = alarms.Current.AlarmFrame;
            //        }
            //        else
            //        {
            //            alarms.Reset();
            //            if (alarms.MoveNext())
            //                image = alarms.Current.AlarmFrame;
            //        }

            //        if (image == null) continue;

            //        int x = index % 2;
            //        int y = index / 2;
            //        e.Graphics.DrawImage(image, new Rectangle(x * 352, y * 288, 352, 288));
            //        index++;
            //    }
            //}
            //catch (Exception Ex)
            //{

            //    MessageBox.Show(Ex.Message);
            //}

        }

        private void tAlarm_Tick(object sender, EventArgs e)
        {
            ucShowDecode1.Invalidate();
        }

        private void tVAreaDevices_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode TN = tVAreaDevices.SelectedNode;
            if (TN == null || TN.Level != 1 || TN.Tag == null) return;

            this.video = TN.Tag as UserVideo;
        }

        #region 云台控制
        private void bCruise_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                if (!this.video.IsCruising)
                    video.StartCruise();
                else
                    this.video.StopCruise();
            }
        }

        private void bUp_MouseDown(object sender, MouseEventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.TiltUp(4);
            }
        }

        private void bDown_MouseDown(object sender, MouseEventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.TiltDown(4);
            }
        }

        private void bLeft_MouseDown(object sender, MouseEventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.PanLeft(4);
            }
        }

        private void bRight_MouseDown(object sender, MouseEventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.PanRight(4);
            }
        }

        private void bStop_MouseUp(object sender, MouseEventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.PTZStop();
            }
        }
        #endregion

        #region 角度管理
        delegate void SetHorizonCallback(object sender, IVideo.HorizonEventArgs e);
        void video_HorizonCallback(object sender, IVideo.HorizonEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetHorizonCallback(video_HorizonCallback), sender, e);
            }
            else
            {
                nUDHorizon.Value = e.Horizon;
            }
        }

        delegate void SetVerticalCallback(object sender, IVideo.VerticalEventArgs e);
        void video_ReceiveVertical(object sender, IVideo.VerticalEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetVerticalCallback(video_ReceiveVertical), sender, e);
            }
            else
            {
                nUDVertical.Value = e.Vertical;
            }
        }

        private void bGoHorizon_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.GoHorizon((int)nUDHorizon.Value);
            }
        }

        private void bGetHorizon_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.AsyncGetHorizon();
            }
        }

        private void bGetVertical_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.GoVertical((int)nUDVertical.Value);
            }
        }
        private void bGoVertical_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.AsyncGetVertical();
            }
        }
        #endregion

        #region 视频管理
        private void cBRecord_CheckedChanged(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                if (cBRecord.Checked)
                    video.StartRecord();
                else
                    video.StopRecord();
            }
        }

        private void bSnap_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                video.SnapVideo();
            }
        }

        private void bAnalyze_Click(object sender, EventArgs e)
        {
            if (video != null && video.IsOpened)
            {
                if (!this.video.IsAnalyzing)
                    video.StartAnalyze();
                else
                    this.video.StopAnalyze();
            }
        }
        #endregion
        


    }
}
